package com.kaili.db.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.client.service.ILoginUserInfoService;
import com.kaili.client.service.IPdaService;
import com.kaili.common.exception.BaseExcept;
import com.kaili.common.util.SnowflakeIdFactory;
import com.kaili.db.mapper.HsOptEwbAbnormalMapper;
import com.kaili.db.service.IHsOptArriveService;
import com.kaili.db.service.IHsOptEwbAbnormalService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.abnormal.EwbAbnormalAddDTO;
import com.kaili.pojo.entity.HsEwbsStowageDetail;
import com.kaili.pojo.entity.HsOptEwbAbnormal;
import com.kaili.pojo.vo.ArriveEwbVO;
import com.kaili.pojo.vo.ewbAbnormalVO;
import com.kaili.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cgm
 * @since 2018-05-08
 */
@Service
public class HsOptEwbAbnormalServiceImpl extends ServiceImpl<HsOptEwbAbnormalMapper, HsOptEwbAbnormal> implements IHsOptEwbAbnormalService {

    @Autowired
    private ILoginUserInfoService loginUserInfoService;
    @Autowired
    private IPdaService pdaService;
    @Autowired
    private IHsOptArriveService hsOptArriveService;

    public int createdEwbAbnormal(HsOptEwbAbnormal hsOptEwbAbnormal){
        //当登记时,处理意见不为空，申请人=处理人
       //String remark = hsOptEwbAbnormal.getHandleSuggestion().trim();
        if ( hsOptEwbAbnormal.getHandleStatus()==1){
            hsOptEwbAbnormal.setHandleBy(hsOptEwbAbnormal.getCreatedBy());
            hsOptEwbAbnormal.setHandleTime(DateUtils.getNowDate());
            hsOptEwbAbnormal.setHandleStatus(1);
        }else {
            hsOptEwbAbnormal.setHandleStatus(0);
        }
        hsOptEwbAbnormal.setCreatedTime(DateUtils.getNowDate());
        baseMapper.createdEwbAbnormal(hsOptEwbAbnormal);

        //修改配载单中的异常件数
        HsEwbsStowageDetail ewbsStowageDetail = new HsEwbsStowageDetail();
        ewbsStowageDetail.setEwbNo(hsOptEwbAbnormal.getEwbNo());
        ewbsStowageDetail.setAbnormalPiece((hsOptEwbAbnormal.getDiffPiece()==null?0:hsOptEwbAbnormal.getDiffPiece()) + (hsOptEwbAbnormal.getDamagePiece()==null?0:hsOptEwbAbnormal.getDamagePiece()));//配载单状态(0-未发 1- 在途 2-已到)
        try{
            hsOptArriveService.updateStowageDetailArrive(ewbsStowageDetail);
        }catch(Exception e){

        }
        return 0;
    }

    /**
     * BS端进行申报异常件
     * @param hsOptEwbAbnormal
     * @return
     * @throws BaseExcept
     */
    @Override
    public int createdEwbAbnormalBS(EwbAbnormalAddDTO ewbAbnormalAddDTO) throws BaseExcept {
        HsOptEwbAbnormal hsOptEwbAbnormal = new HsOptEwbAbnormal();
        BeanUtils.copyProperties(ewbAbnormalAddDTO, hsOptEwbAbnormal);
        hsOptEwbAbnormal.setDeviceCode("BS");
        hsOptEwbAbnormal.setScanSourceId(0);
        String operationId = SnowflakeIdFactory.getNextId();
        hsOptEwbAbnormal.setOperationId(operationId);
        hsOptEwbAbnormal.setCreatedBy(loginUserInfoService.getLoginUserId());
        //hsOptEwbAbnormal.setSiteId(loginUserInfoService.getLoginSiteId());

        return createdEwbAbnormal(hsOptEwbAbnormal);
    }

    /**
     * APP专属接口/异常申报
     * @param hsOptEwbAbnormal
     * @return
     * @throws BaseExcept
     */
    @Override
    public int createdEwbAbnormalAPP(HsOptEwbAbnormal hsOptEwbAbnormal) throws BaseExcept {
        if (hsOptEwbAbnormal.getDeviceCode() !=null || hsOptEwbAbnormal.getDeviceCode() !=""){
            return -1;
        }
        hsOptEwbAbnormal.setScanSourceId(pdaService.getSouceIdByCode(hsOptEwbAbnormal.getDeviceCode()));
        return createdEwbAbnormal(hsOptEwbAbnormal);
    }

    /**
     * 修改异常单
     * @param hsOptEwbAbnormal
     * @return
     * @throws BaseExcept
     */
    @Override
    public int modifyAbnormal(EwbAbnormalAddDTO ewbAbnormalDTO) throws BaseExcept {
        HsOptEwbAbnormal hsOptEwbAbnormal = new HsOptEwbAbnormal();
        BeanUtils.copyProperties(ewbAbnormalDTO, hsOptEwbAbnormal);
        //String handleSuggestion = hsOptEwbAbnormal.getHandleSuggestion().trim();
        ewbAbnormalVO abnormal = getAbnormalByEwbNo(hsOptEwbAbnormal);
        if (abnormal == null ){
           throw new BaseExcept(ResponseStatus.EXCEPTION_OBTAIN_ERR);
        }
        hsOptEwbAbnormal.setModifiedBy(loginUserInfoService.getLoginUserId());
        hsOptEwbAbnormal.setModifiedTime(DateUtils.getNowDate());
        //判断是否做了处理操作
        if (hsOptEwbAbnormal.getHandleStatus()==1){
            hsOptEwbAbnormal.setHandleBy(loginUserInfoService.getLoginUserId());
            hsOptEwbAbnormal.setHandleTime(DateUtils.getNowDate());
            hsOptEwbAbnormal.setHandleStatus(1);
        }
        baseMapper.modifyAbnormals(hsOptEwbAbnormal);

        return 0;
    }

    /**
     * 删除异常单
     * @param hsOptEwbAbnormal
     * @return
     * @throws BaseExcept
     */
    @Override
    public int deleteAbnormalByEwb(HsOptEwbAbnormal hsOptEwbAbnormal) throws BaseExcept {
        return baseMapper.deleteAbnormalByEwb(hsOptEwbAbnormal);
    }

    /**
     * 批量查询
     * @param page
     * @param map
     * @return
     * @throws BaseExcept
     */
    @Override
    public Page<ewbAbnormalVO> queryAbnormal(Page<ewbAbnormalVO> page, Map map) throws BaseExcept {

        return page.setRecords(baseMapper.queryEwbAbnormalList(page,map));
    }

    @Override
    public ewbAbnormalVO getAbnormalByEwbNo(HsOptEwbAbnormal hsOptEwbAbnormal) throws BaseExcept {
        return baseMapper.getAbnormalByEwbNo(hsOptEwbAbnormal);
    }

    @Override
    public ewbAbnormalVO queryAbnormalByEwbNo(EwbAbnormalAddDTO hsOptEwbAbnormal) throws BaseExcept {
        return baseMapper.queryAbnormalByEwbNo(hsOptEwbAbnormal);
    }

    @Override
    public List<ArriveEwbVO> queryOriginalEwb(Map<String, Object> map) throws BaseExcept {
        return baseMapper.queryOriginalEwb(map);
    }

    /**
     * 导出服务类
     * @param map
     * @return
     * @throws BaseExcept
     */
    @Override
    public List<ewbAbnormalVO> exportAbnormalQueryList(Map map) throws BaseExcept {
        return baseMapper.exportAbnormalQueryList(map);
    }
}
